package org.truenewx.tnxjee.repo.mybatis;

import java.io.Serializable;

import org.truenewx.tnxjee.core.util.ClassUtil;
import org.truenewx.tnxjee.model.entity.Entity;
import org.truenewx.tnxjee.model.entity.relation.Relation;
import org.truenewx.tnxjee.model.entity.relation.RelationKey;
import org.truenewx.tnxjee.repo.mybatis.mapper.EntityMapper;
import org.truenewx.tnxjee.repo.mybatis.mapper.RelationMapper;

public class MyBatisRelationMapperRepository<T extends Relation<L, R>, L extends Serializable, R extends Serializable>
        extends MyBatisEntityMapperRepository<T, RelationKey<L, R>> {

    private final RelationMapper<T, L, R> mapper;

    public MyBatisRelationMapperRepository(RelationMapper<T, L, R> mapper) {
        this.mapper = mapper;
    }

    @Override
    protected EntityMapper<T> getMapper() {
        return this.mapper;
    }

    @Override
    public Class<? extends Entity> getEntityClass() {
        return ClassUtil.getActualGenericType(this.mapper.getClass(), RelationMapper.class, 0);
    }

    @Override
    public boolean existsById(RelationKey<L, R> id) {
        return this.mapper.existsById(id.getLeft(), id.getRight());
    }

}
